<?php
/**
 * made by : stupboy
 */
namespace app\api\model;
use think\Db;
class CarType extends Common
{
    public function getType($goType,$useType){
        if($goType == 2 || $goType==4){
            return $this->getDailyType($goType,$useType);
        }else{
            return $this->getCharteredType($goType,$useType);
        }
    }

    public function getDailyType($goType,$useType){
        $field         = 'max(car_type_id) car_type_id,car_type,type';
        $carTypes      = config('base.car_types');
        $where['type'] = $goType;//2,3
        if($useType==1){
            $carLimit = Db::name('car')->where('prop',3)->column('car_type_id');
            $where['car_type_id'] = ['in',implode(',',$carLimit)];
        }
        $carType       = $this->where($where)->field($field)->group('car_type,type')->whereNull('delete_time')->order('chartered_hour asc')->select();
        $rank          = [2=>1,3=>2,4=>3,9=>4,5=>5,6=>6,7=>7,8=>8];
        foreach ($carType as $k=>$v){
            $carType[$k]['car_type_name'] = $carTypes[$v['car_type']];
            $carType[$k]['rank']          = $rank[$v['car_type']];
        }
        return $carType;
    }

    public function getCharteredType($goType,$useType){
        $field         = 'car_type_id,car_type,type,chartered_km,p_chartered_charge chartered_charge,chartered_hour,seats';
        $carTypes      = config('base.car_types');
        $where['type'] = $goType;//2,3
        $carType       = $this->where($where)->field($field)->whereNull('delete_time')->order('chartered_hour asc')->select();
        $rank          = [2=>1,3=>2,4=>3,9=>4,5=>5,6=>6,7=>7,8=>8];
        $newType       = [];
        foreach ($carType as $k=>$v){
            $newType[$v['car_type']]['name']    = $carTypes[$v['car_type']];
            $newType[$v['car_type']]['rank']    = $rank[$v['car_type']];
            $newType[$v['car_type']]['types'][] = $v;
        }
        return $newType;
    }
}